﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using Turquoise.Core.Abstract;
using Turquoise.Providers.Abstract;
using Turquoise.Providers.EntityFramework.Abstract;

namespace Turquoise.Providers.EntityFramework
{
    public class Repository<TEntity> : IRepository<TEntity> where TEntity : class, IEntity, new()
    {
        private readonly IDatabaseContext _context;
        private bool _disposed;
        private IDbSet<TEntity> TableCollection => _context.Set<TEntity>();

        public Repository(IDatabaseContext context)
        {
            _context = context;
        }

        public IQueryable<TEntity> DbSet()
        {
            return TableCollection;
        }
        public List<TEntity> GetList()
        {
            return _context.Set<TEntity>().ToList();
        }

        public List<TEntity> GetList(Expression<Func<TEntity, bool>> whereExpression)
        {
            return _context.Set<TEntity>().Where(whereExpression).ToList();
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {
                    _context.Dispose();
                }
            }
            _disposed = true;
        }
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}
